import axios from 'axios'
import {history} from "umi";

import {getItem} from "../pages/models/storage";
import {message} from "antd";

const request = axios.create({
  baseURL: 'http://localhost:8501',
  timeout: 10000
})

request.interceptors.request.use((config) => {
    // config.headers.Authorization = `Bearer ${authorization.token}`
  if(getItem("TOKEN").password
    && (window.location.href !== "http://localhost:8000/login"
    || window.location.href !== "http://localhost:8000/lock")) {
    history.push("/lock")
  }
  if(!getItem("TOKEN")) {
    message.error("用户未登录")
    setTimeout(() => {
      history.push('/login')
    }, 1500)
  }
  config.headers.token = `${getItem("TOKEN").token}`
  return config
}, (error) => {
  return Promise.reject(error)
})

request.interceptors.response.use((response) => {
  const {code} = response
  if(code === 208) {
    message.error("用户未登录")
    setTimeout(() => {
      history.push('/login')
    }, 1500)
  }
  return response.data
}, (error => {
  return Promise.reject(error)
}))

export {request}
